/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.business;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;

/**
 * 添加联系人Controller
 * 
 * @author 周豫陈
 * @version 2016-12-05
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/business/hycrmContact")
public class HycrmContactController extends BaseController {

	@Autowired
	private HycrmContactService hycrmContactService;

	@ModelAttribute
	public HycrmContact get(@RequestParam(required = false) String id) {
		HycrmContact entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = hycrmContactService.get(id);
		}
		if (entity == null) {
			entity = new HycrmContact();
		}
		return entity;
	}

	// 进入添加联系人页面
	@RequestMapping(value = "form")
	public String form(HycrmContact hycrmContact, Model model) {
		model.addAttribute("hycrmContact", hycrmContact);
		return "modules/crm/business/hycrmContactForm";
	}

	// 进入修改联系人页面
	@RequestMapping(value = "formChange")
	public String formChange(HycrmContact hycrmContact, Model model) {
		model.addAttribute("hycrmContact", hycrmContact);
		return "modules/crm/business/hycrmContactFormChange";
	}

	// 保存或修改联系人
	@RequestMapping(value = "save")
	public String save(HycrmContact hycrmContact, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hycrmContact)) {
			return form(hycrmContact, model);
		}
		hycrmContactService.save(hycrmContact);
		addMessage(redirectAttributes, "保存联系人成功");
		return "redirect:" + Global.getAdminPath()
				+ "/crm/findcustomer/findCustomer/form?id="
				+ hycrmContact.getCustomerId();
	}

	// 删除联系人
	@RequestMapping(value = "delete")
	public String delete(HycrmContact hycrmContact,
			RedirectAttributes redirectAttributes) {
		List<HycrmContact> findContact = hycrmContactService
				.findContact(hycrmContact);
		if (findContact.size() > 0) {
			addMessage(redirectAttributes, "删除联系人失败！该联系人有项目");
			return "redirect:" + Global.getAdminPath()
					+ "/crm/findcustomer/findCustomer/form?id="
					+ hycrmContact.getCustomerId();
		} else {
			hycrmContactService.delete(hycrmContact);
			addMessage(redirectAttributes, "删除联系人成功");
			return "redirect:" + Global.getAdminPath()
					+ "/crm/findcustomer/findCustomer/form?id="
					+ hycrmContact.getCustomerId();
		}
	}

	// 匹配手机号，查重
	@RequestMapping(value = "findMobile")
	public void findMobile(@RequestParam("mobile") String mobile,
			HttpServletResponse response) {
		String[] mobiles = mobile.split(",");
		boolean b = true;
		for (int i = 0; i < mobiles.length; i++) {
			List<HycrmContact> hc = hycrmContactService.findMobile(mobiles[i]);
			if (hc.size() > 0) {
				b = false;
			}
		}

		response.setContentType("application/text;charset=utf-8");
		try {
			if (b) {
				response.getWriter().print("false");
			} else {
				response.getWriter().print("true");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}